package com.touwowo.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) {
        // 创建连接工厂，设置HOST，username、password信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = null;
        /**
         * 定义消费成功回调函数
         */
        DeliverCallback deliverCallback = (consumerTag,message) -> {
            System.out.println(new String(message.getBody()));
        };

        /**
         * 定义取消消费的回调函数
         */
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("取消消费。");
        };
        // 获取连接
        try {
            connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}
